<template>
    <div class="app-container">
      <nabtwo index="4" :params="param"></nabtwo>
      <!-- 搜索栏部分 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="frominfo">
        <div class="from-item">
          <el-form-item label="类型" prop="termType">
            <el-select v-model="queryParams.termType" placeholder="请选择类型" clearable>
              <el-option v-for="dict in dict.type.sys_greet" :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </div>
  
        <div class="from-Btn">
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </div>
  
      </el-form>
      <!-- 新增按钮操作 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增快捷语</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <!-- 信息部分框 -->
      <el-row :gutter="20" class="card" v-if="infoList!=''&&infoList!=null">
        <el-col :span="8" v-for="(item, index) in infoList" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <div class="box-hea-btn" :class="item.isDefault==1?'btn-bg2':'btn-bg1'" @click="setDefault(item)">{{item.isDefault==1?'取消默认':'设为默认'}}</div>
              <div style="display: flex;">
                <img style="height: 30px;width: 65px;" src="../../../../assets/images/Ytype.png"/>
                <span style="line-height: 30px;font-size: 14px;font-weight: 600;margin-left: 20px;">
                    <dict-tag :options="dict.type.sys_greet" :value="item.termType"/>
                </span>
              </div>
              <div class="ass">{{ item.content }}</div>
            </div>
            <div>
              <div class="text item">
                <el-button style="float: right; padding: 0px 10px; margin-right: 10px;margin-top: -5px;" type="text"
                  icon="el-icon-delete" @click="handleDelete(item)"></el-button>
                <el-button style="float: right; padding: 0px 0;margin-top: -5px;" type="text" icon="el-icon-edit"
                  @click="handleUpdate(item)"></el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <el-empty v-if="infoList==''||infoList==null" description="暂无数据"></el-empty>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
  
      <!-- 添加或修改助手信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="类型" prop="termType">
            <el-select v-model="form.termType" placeholder="请选择类型" clearable>
              <el-option v-for="dict in dict.type.sys_greet" :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input v-model="form.content" type="textarea" :maxlength="460" rows="6" placeholder="请输入内容" />
          </el-form-item>         
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
    
  <script>
  
  import request from '@/utils/request'
  import nabtwo from '../../components/nabtwo.vue'
  export default {
    name: "WcAssistGreetInfo",//
    dicts: ['sys_greet'],
    components: {
      nabtwo
    },
    data() {
      return {
        param: { wxid:this.$route.query.wxid,userid:this.$route.query.userid},
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          wcId: this.$route.query.wxid,
          termType: null,
        },
        // 表单参数
        form: {
          wcId: this.$route.query.wxid,
          termType: null,
          content: null,
          isDefault:0,
        },
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();  
    },
    methods: {
      /** 查询助手招呼语设置列表 */
      getList() {
        this.loading = true;
        page(this.queryParams).then(res => {
          if (res['code']==200) {
            const dt = res['data'];
            this.total = dt['total'];
            this.infoList = dt['records'];
          }
          this.loading = false;
        });
        setTimeout(()=>{
          this.loading = false;
        },2000)
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          wcId: this.$route.query.wxid,
          termType: null,
          content: null,
          isDefault:0,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        // 只重置queryParams中的搜索相关属性
        
        // 重置表单
        this.resetForm("queryForm");
        this.queryParams.wcId=this.$route.query.wxid
        // 重新获取数据列表
        this.getList();
      },
  
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.termId)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增快捷语";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.termId || this.ids;
        queryById(id).then(response => {
          //console.log(response)
          response.data.isDefault=response.data.isDefault==1?true:false
          response.data.termType=String(response.data.termType)
          this.form = response.data;
          this.open = true;
          this.title = "修改快捷语";
        });
      },
      setDefault(arr){
        var form = {
          termId:arr.termId,
          wcId: this.$route.query.wxid,
          termType: arr.termType,
          content: arr.content,
          isDefault:arr.isDefault==1?0:1,
        };
        edits(form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      },
      /** 提交按钮 */
      submitForm() {
        if(this.form.isDefault){
          this.form.isDefault=1
        }else{
          this.form.isDefault=0
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.termId != null) {
              edits(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              adds(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.termId || this.ids;
        this.$modal.confirm('是否确认删除快捷语？').then(function () {
          return deletes(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
    }
  };
  
  /** js请求 调用 */
  
  // 查询助手招呼语设置列表
  export function page(query) {
    return request({
      url: '/wcUserTerms/page',
      method: 'get',
      params: query
    })
  }
  
  // 查询助手招呼语设置详细
  export function queryById(id) {
    return request({
      url: '/wcUserTerms/queryById/' + id,
      method: 'get'
    })
  }
  
  // 新增助手招呼语设置
  export function adds(data) {
    return request({
      url: '/wcUserTerms/add',
      method: 'post',
      data: data
    })
  }
  
  // 修改助手招呼语设置
  export function edits(data) {
    return request({
      url: '/wcUserTerms/edit',
      method: 'put',
      data: data
    })
  }
  
  // 删除助手招呼语设置
  export function deletes(id) {
    return request({
      url: '/wcUserTerms/delete/' + id,
      method: 'delete'
    })
  }
  
  </script>
  <style lang="scss" scoped>
  .box-card {
    margin-bottom: 20px;
  }
  .frominfo{
        display: flex;background-color: #fff;padding: 10px;padding-bottom: 0; margin-bottom: 20px;
        .from-item{
          display: flex;flex-wrap: wrap;width: 80%;
        }
        .from-Btn{
          display: flex;flex-direction: column;align-items: center;justify-content: center;width: 20%;
        }
  }
  .clearfix {
    height: 145px;
    .box-hea-btn{
      font-size: 14px;height: 25px;width: 70px;border-radius: 5px;line-height: 25px;text-align: center;color: #fff;margin-left: 74%;
    }
    .btn-bg1{
      background-color: rgb(12, 112, 228);
    }
    .btn-bg2{
      background-color:#b6c1cd;
    }
    span {
      //font-weight: bold;
  
      i {
        color: rgb(12, 112, 228);
        margin-right: 20px;
      }
    }
  
    .ass {
      font-size: 12px;
      color: rgb(184, 181, 178);
      margin-top: 20px;
      margin-left:5px;
      display: -webkit-box; // 将对象作为弹性伸缩盒子模型显示
      -webkit-box-orient: vertical; // 设置或检索伸缩盒对象的子元素的排列方式
      -webkit-line-clamp: 4; // 显示两行
      overflow: hidden;
    }
  }
  
  
  .app-container {
    background-color: #f6f9fb;
  }
  
  .el-col-8 {
    width: 320px;
  }
  
  </style>